package com.heima.common.exception;

import com.heima.model.common.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器，处理项目中抛出的业务异常
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理不可控异常
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseResult exception(Exception e) {
        e.printStackTrace();
        log.error("处理不可控异常:{}", e.getMessage());
        return ResponseResult.error(e.getMessage());
    }

    /**
     * 处理可控异常  自定义异常
     */
    @ExceptionHandler(BaseException.class)
    @ResponseBody
    public ResponseResult exception(BaseException e) {
        log.error("处理可控异常:{}", e.getMessage());
        return ResponseResult.error(e.getMessage());
    }

}
